//
//  JVerdorHeadView.m
//  HeadIndustry
//
//  Created by jun on 2017/9/14.
//  Copyright © 2017年 jun. All rights reserved.
//

#import "JVerdorHeadView.h"
#import <SDCycleScrollView.h>

#define msgBtnTag 10

@interface JVerdorHeadView()<SDCycleScrollViewDelegate>


@property (nonatomic, strong) CALayer *headImgLayer;
@property (nonatomic, strong) YYLabel *verdorName;
@property (nonatomic, strong) UIButton *collectBtn;
@property (nonatomic, strong) YYLabel *productCount;
@property (nonatomic, strong) YYLabel *saleCount;
@property (nonatomic, strong) YYLabel *collectCount;
@property (nonatomic, strong) UIView  *lineView;
@property (nonatomic, strong) NSMutableArray *btnArr;
@property (nonatomic, strong) SDCycleScrollView *cycleView;

@property (nonatomic, assign) NSInteger isDown; // 1 下， 2 上
@property (nonatomic, assign) NSInteger lestSelnum;
@end

@implementation JVerdorHeadView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = kRGBColor(245, 245, 245);
        self.opaque = NO;
        _btnArr = [NSMutableArray array];
        _isDown = 1;
        // 轮播图
        [self setUpRunLoopImg];
        
        [self setUpView];
        
        
        
    }
    return  self;
}

// 轮播图
- (void)setUpRunLoopImg
{
    NSArray *images = @[
                        [UIImage imageNamed:@"banner1"]
                        ];
    
    
    // 本地加载图片的轮播器
    _cycleView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, self.width, self.height - 100) imageNamesGroup:images];
    _cycleView.backgroundColor = [UIColor whiteColor];
    
    _cycleView.autoScrollTimeInterval = images.count;// 自定义轮播时间间隔
    
    _cycleView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    _cycleView.pageControlStyle = SDCycleScrollViewPageContolStyleClassic;
    _cycleView.showPageControl = YES;
    _cycleView.pageControlDotSize = CGSizeMake(10, 30);
    _cycleView.pageDotColor = [UIColor grayColor];
    _cycleView.currentPageDotColor = [UIColor whiteColor];
    //NSArray*titles = @[@"山东华网智能科技股份有限公司",@"山东华网智能科技股份有限公司",@"山东华网智能科技股份有限公司"];
    //cycleScrollView.titlesGroup = titles;
    
    _cycleView.delegate = self;
    [self addSubview:_cycleView];
    
    CALayer *garylayer = [CALayer layer];
    garylayer.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3].CGColor;
    garylayer.frame = _cycleView.frame;
    [self.layer addSublayer:garylayer];
}

- (void)setUpView {
    
    CALayer *headLayer = [CALayer layer];
    headLayer.frame = CGRectMake(self.width/2.0 - 30, 30, 60, 60);
    headLayer.cornerRadius = 30;
    headLayer.masksToBounds = YES;
    headLayer.contents = (__bridge id _Nullable)([UIImage imageNamed:@""].CGImage);
    [self.layer addSublayer:headLayer];
    _headImgLayer = headLayer;
    
    YYLabel *verdorName = [YYLabel new];
    verdorName.frame = CGRectMake(0, headLayer.bottom + 10, self.width, 20);
    verdorName.font = kFONT_TITLE(18);
    verdorName.textColor = [UIColor whiteColor];
    verdorName.textAlignment = NSTextAlignmentCenter;
    [self addSubview:verdorName];
    _verdorName = verdorName;
    
    UIButton *msgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    msgBtn.frame = CGRectMake(self.width / 2.0 - 10 - 100 , verdorName.bottom + 10, 100, 35);
    msgBtn.titleLabel.font = kFONT_TITLE(16);
    [msgBtn setImage:[UIImage imageNamed:@"whitespeak_icon"] forState:UIControlStateNormal];
    [msgBtn setTitle:@" 联系客服" forState:UIControlStateNormal];
    msgBtn.tag = msgBtnTag;
    msgBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    msgBtn.layer.borderWidth = 1;
    msgBtn.layer.cornerRadius = 3;
    [msgBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:msgBtn];
    
    UIButton *collectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    collectBtn.frame = CGRectMake(self.width / 2.0 + 10 , verdorName.bottom + 10, 100, 35);
    collectBtn.titleLabel.font = kFONT_TITLE(16);
    [collectBtn setImage:[UIImage imageNamed:@"whitestar"] forState:UIControlStateNormal];
    [collectBtn setImage:[UIImage imageNamed:@"red_star_verdor"] forState:UIControlStateSelected];

    [collectBtn setTitle:@" 收藏店铺" forState:UIControlStateNormal];
    [collectBtn setTitle:@" 取消收藏" forState:UIControlStateSelected];
    collectBtn.tag = msgBtnTag + 1;
    collectBtn.layer.cornerRadius = 3;
    collectBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    collectBtn.layer.borderWidth = 1;
    [collectBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:collectBtn];
    _collectBtn = collectBtn;
    
    UIView *labelbgView = [[UIView alloc]initWithFrame:CGRectMake(0, _cycleView.bottom, self.width, 50)];
    labelbgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:labelbgView];
    
    CGFloat width = self.width / 3.0;
    
    YYLabel *productCount = [YYLabel new];
    productCount.frame = CGRectMake(0, 0, width, 20);
    productCount.textAlignment = NSTextAlignmentCenter;
    productCount.font = kFONT_TITLE(12);
    [labelbgView addSubview:productCount];
    _productCount = productCount;
    
    YYLabel *productCountText = [YYLabel new];
    productCountText.frame = CGRectMake(0, productCount.bottom, width, 30);
    productCountText.textAlignment = NSTextAlignmentCenter;
    productCountText.font = kFONT_TITLE(14);
    productCountText.text = @"商品";
    productCountText.textColor = kRGBColor(150, 150, 150);
    [labelbgView addSubview:productCountText];
    
    YYLabel *saleCount = [YYLabel new];
    saleCount.frame = CGRectMake(width, 0, width, 20);
    saleCount.textAlignment = NSTextAlignmentCenter;
    saleCount.font = kFONT_TITLE(12);
    [labelbgView addSubview:saleCount];
    _saleCount = saleCount;
    
    YYLabel *saleCountText = [YYLabel new];
    saleCountText.frame = CGRectMake(width, saleCount.bottom, width, 30);
    saleCountText.textAlignment = NSTextAlignmentCenter;
    saleCountText.font = kFONT_TITLE(14);
    saleCountText.text = @"销量";
    saleCountText.textColor = kRGBColor(150, 150, 150);
    [labelbgView addSubview:saleCountText];
    
    YYLabel *collectCount = [YYLabel new];
    collectCount.frame = CGRectMake(width * 2, 0, width, 20);
    collectCount.textAlignment = NSTextAlignmentCenter;
    collectCount.font = kFONT_TITLE(12);
    [labelbgView addSubview:collectCount];
    _collectCount = collectCount;
    
    YYLabel *collectCountText = [YYLabel new];
    collectCountText.frame = CGRectMake(width * 2, productCount.bottom, width, 30);
    collectCountText.textAlignment = NSTextAlignmentCenter;
    collectCountText.font = kFONT_TITLE(14);
    collectCountText.text = @"粉丝";
    collectCountText.textColor = kRGBColor(150, 150, 150);
    [labelbgView addSubview:collectCountText];
    
    
    for (int i = 1; i < 3; i ++) {
        CALayer *line = [CALayer layer];
        line.backgroundColor = kRGBColor(150, 150, 150).CGColor;
        line.frame = CGRectMake(kWindowW/3.0 * i,  5, 1, 40);
        [labelbgView.layer addSublayer:line];
    }
    
    UIView *bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, labelbgView.bottom+10, self.width, 40)];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self addSubview:bottomView];
    
    NSArray *nameArr = @[@"默认",@"最新",@"热销",@"价格"];
    CGFloat selbtnWidth = self.width/4.0;
    
    for (int i = 0; i < nameArr.count; i++) {
        UIButton *collectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        collectBtn.frame = CGRectMake(selbtnWidth * i , 0, selbtnWidth, 40);
        collectBtn.titleLabel.font = kFONT_TITLE(14);
        [collectBtn setTitle:nameArr[i] forState:UIControlStateNormal];
        [collectBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [collectBtn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        collectBtn.tag = msgBtnTag + 10 + i;

        [collectBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        if (i == 0) {
            collectBtn.selected = YES;
        }
        
        if (i == 3) {
            if (_isDown == 1) {
                [collectBtn setImage:[UIImage imageNamed:@"down_1"] forState:UIControlStateNormal];
            }
            else
            {
                [collectBtn setImage:[UIImage imageNamed:@"up_1"] forState:UIControlStateNormal];
            }
            
            [collectBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -20, 0, 0)];
            [collectBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 63, 0, 5)];

        }
        
        [bottomView addSubview:collectBtn];
        [_btnArr addObject:collectBtn];
    }
    
    _lineView = [[UIView alloc]initWithFrame:CGRectMake((selbtnWidth - 30)/2.0, 39, 30, 1)];
    _lineView.backgroundColor = [UIColor redColor];
    [bottomView addSubview:_lineView];
    
}

- (void)setVerdorModel:(JVerdorDetailModel *)verdorModel
{
    _verdorModel = verdorModel;
    
    if (verdorModel.IsCollected == 1) {
        _isCollect = YES;
    }
    else
    {
        _isCollect = NO;
    }
    
    _collectBtn.selected = _isCollect;
    _verdorName.text = verdorModel.Name;
    _productCount.text = [NSString stringWithFormat:@"%@",verdorModel.ProductCount];
    _saleCount.text = [NSString stringWithFormat:@"%@",verdorModel.SaleCount];
    _collectCount.text = [NSString stringWithFormat:@"%@",verdorModel.CollectCount];
    UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:verdorModel.LogoUrl]]];
    _headImgLayer.contents = (__bridge id _Nullable)(img.CGImage);
    
    
    if (_verdorModel.BannerPictures.count > 0) {
        NSMutableArray *imgs = [NSMutableArray array];
        
        for (JVerdorBannersImgModel *model in _verdorModel.BannerPictureModels) {

            [imgs addObject:model.ImgUrl];
        }
        _cycleView.autoScrollTimeInterval = _verdorModel.BannerPictures.count;
        dispatch_async(dispatch_get_main_queue(), ^{
            _cycleView.imageURLStringsGroup = imgs;
        });
    }
    
}

- (void)setIsCollect:(BOOL)isCollect
{
    _isCollect = isCollect;
    _collectBtn.selected = isCollect;
}

- (void)setCollectCountInt:(NSInteger)collectCountInt
{
    _collectCountInt = collectCountInt;
    _collectCount.text = [NSString stringWithFormat:@"%ld",(long)_collectCountInt];
}

- (void)btnClick:(UIButton *)btn
{
    if (btn.tag > 15) {
        if (btn.tag == msgBtnTag + 10 + 3) {
            if (btn.tag == _lestSelnum) {
                if (_isDown == 1) {
                    _isDown = 2 ;
                }
                else
                {
                    _isDown = 1;
                }
            }
            
            if (_isDown == 1) {
                [btn setImage:[UIImage imageNamed:@"down_1"] forState:UIControlStateNormal];
            }
            else
            {
                [btn setImage:[UIImage imageNamed:@"up_1"] forState:UIControlStateNormal];
            }
        }
        else
        {
            if (btn.tag == _lestSelnum) {
                return;
            }
        }
        
        for (UIButton *btn in self.btnArr) {
            btn.selected = NO;
        }
        btn.selected = YES;
        [self lineViewAnimation:btn.tag - 20];
        
        _lestSelnum = btn.tag;
    }
    
    if (btn.tag == msgBtnTag + 10 + 3) {
        if (_verdorBtnBlock) {
            _verdorBtnBlock(btn.tag,self.verdorModel,_isDown);
        }
    }
    else
    {
        if (_verdorBtnBlock) {
            _verdorBtnBlock(btn.tag,self.verdorModel,0);
        }
    }
}

- (void)lineViewAnimation:(NSInteger)num
{
    
    [UIView animateWithDuration:0.3 animations:^{
        _lineView.frame = CGRectMake((self.width/4.0 - 30)/2.0 + num * self.width/4.0, _lineView.origin.y, _lineView.width, _lineView.height);
    }];
}


@end
